package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.ResearchDirectionMapper;
import com.bjfu.gmms.mapper.StudentAdmissionInfoMapper;
import com.bjfu.gmms.model.ResearchDirection;
import com.bjfu.gmms.model.StudentAdmissionInfo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/fillStudentVolunteer")
public class FillStudentVolunteerServlet extends HttpServlet {
    private StudentAdmissionInfoMapper studentAdmissionInfoMapper;
    private ResearchDirectionMapper researchDirectionMapper;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int studentId = Integer.parseInt(request.getParameter("studentId"));
        SqlSession sqlSession = MyBatisUtil.getSqlSession("development");
        studentAdmissionInfoMapper = sqlSession.getMapper(StudentAdmissionInfoMapper.class);
        researchDirectionMapper = sqlSession.getMapper(ResearchDirectionMapper.class);
        // 调用存储过程查询考生信息
        StudentAdmissionInfo studentAdmissionInfo = studentAdmissionInfoMapper.getStudentAdmissionInfo(studentId);
        if (studentAdmissionInfo == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND, "未找到该考生的信息");
            return;
        }
        System.out.println("录取id是"+studentAdmissionInfo.getAdmittedSecondaryDisciplineId());
        List<ResearchDirection> researchDirections = researchDirectionMapper.getResearchDirectionsByDiscipline(
                studentAdmissionInfo.getAdmittedSecondaryDisciplineId());

        // 将查询结果传递给 JSP 页面
        request.setAttribute("studentAdmissionInfo", studentAdmissionInfo);
        request.setAttribute("researchDirections", researchDirections);
        request.getRequestDispatcher("volunteerSystem.jsp").forward(request, response);
    }
}
